package net.wapproject.dao.impl;

import java.util.List;

import javax.persistence.OrderBy;

import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import net.wapproject.bean.Category;
import net.wapproject.bean.Game;
import net.wapproject.dao.IGameDAO;

public class GameDaoImpl extends HibernateDaoSupport implements IGameDAO {

	@Override
	public List<Game> getGameListByCategory(Integer categoryId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Game> getGameListByCategory(Integer categoryId,
			Integer currentPage) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Game> getGameListByCategoryRoot(Integer categoryRootId) {
		Criteria criteria = getSession().createCriteria(Game.class);
		criteria.add(Expression.eq("rootCategory", categoryRootId)).addOrder(Order.asc("createDate"));
		criteria.setFirstResult(0);
		criteria.setMaxResults(10);
		return criteria.list();
	}

	@Override
	public Game getGameByGameId(Integer gameId) {
		Criteria criteria = getSession().createCriteria(Game.class);
		criteria.add(Expression.eq("id", gameId));
		return (Game) criteria.list().get(0);
	}

	@Override
	public void updateTotalView(Integer gameId) {
		Game game = getGameByGameId(gameId);
		game.setTotalView(game.getTotalView() + 1);
		getSessionFactory().openSession().saveOrUpdate(game);
	}

	@Override
	public List<Game> gameListByCategoryRoot(Category category) {
		Criteria criteria = getSession().createCriteria(Game.class);
		criteria.add(Expression.eq("rootCategory", category.getId())).addOrder(Order.asc("createDate"));
		//criteria.setFirstResult();
		criteria.setMaxResults(10);
		return criteria.list();
	}
	
	

}
